import 'package:flutter/material.dart';
import 'musicItem.dart';

class ImportScreen extends StatefulWidget {
  const ImportScreen({super.key});
  @override
  State<ImportScreen> createState() => _ImportScreenState();
}

class _ImportScreenState extends State<ImportScreen> {
  bool _isInitialized = false;

  @override
  void initState() {
    super.initState();
    _initializeData();
  }

  void _initializeData() {
    if (!_isInitialized) {
      testMusicItem();
      _isInitialized = true;
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My Music',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
      ),
      home: Scaffold(
          appBar: AppBar(
            title: const Text("我的歌单",
              style: TextStyle(
                fontSize: 30,
                fontWeight: FontWeight.bold,
              ),
            ),
            actions: [
              IconButton(
                icon: const Icon(Icons.add),
                onPressed: () {
                  print("add MusicList");
                  // 添加音乐列表的逻辑
                },
              ),
              IconButton(
                icon: const Icon(Icons.delete),
                onPressed: () {
                  print("delete MusicList");
                  // 删除音乐列表的逻辑
                },
              ),
            ],
          ),

          body: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // 添加标题
              GestureDetector(
                onTap: () {
                  print("点击 所有歌曲");
                },
                child: Container(
                  height: 100,
                  child: Padding(
                    padding: EdgeInsets.symmetric(horizontal: 16, vertical: 2),
                    child: Card(
                      elevation: 4, // 添加阴影
                      child: Row(
                        children: [
                          Padding(
                            padding: EdgeInsets.all(16),
                            child: Icon(Icons.music_note, size: 40), // 增大图标
                          ),
                          Text(
                            "所有歌曲",
                            style: TextStyle(
                              fontSize: 24,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
              ),
              // 列表内容
              Expanded(
                child: ListView(
                  children: buildAlbumList(),
                ),
              ),
            ],
          )

      ),
    );
  }

  List<Widget> buildAlbumList() {
    List<Widget> albumWidget = [];
    AllAlbum allAlbum = AllAlbum.instance;
    allAlbum.albumList.forEach((album) {
      albumWidget.add(
          GestureDetector(
            onTap: () {
              print("点击歌单：${album.albumName}");
            },
            child: Card(
              elevation: 4,
              margin: EdgeInsets.symmetric(horizontal: 16, vertical: 2),
              child: Container(
                height: 100,
                child: Row(
                  children: [
                    Container(
                      width: 80,
                      height: 80,
                      child: ClipRRect(
                        borderRadius: BorderRadius.only(
                          topLeft: Radius.circular(4),
                          bottomLeft: Radius.circular(4),
                        ),
                        child: Image.asset(
                          album.albumCover,
                          fit: BoxFit.cover,
                        ),
                      ),
                    ),
                    SizedBox(width: 16),
                    Expanded(
                      child: Padding(
                        padding: EdgeInsets.symmetric(vertical: 8), // 减少垂直内边距
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start, // 左对齐
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly, // 均匀分布
                          children: [
                            Text(
                              album.albumName,
                              style: TextStyle(
                                fontSize: 20, // 略微缩减字体大小
                                fontWeight: FontWeight.w500,
                              ),
                              maxLines: 1, // 限制为单行
                              overflow: TextOverflow.ellipsis, // 超出显示省略号
                            ),
                            Text(
                              album.musicList.isNotEmpty
                                  ? "${album.musicList.first.musicName} - ${album.musicList.first.musicAuthor}"
                                  : "无歌曲",
                              style: TextStyle(fontSize: 14), // 使用较小字体
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                            ),
                            Text(
                              "${album.musicList.length} 首",
                              style: TextStyle(fontSize: 14, color: Colors.grey),
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                            ),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          )
      );
    });
    return albumWidget;
  }

  void testMusicItem() {
    final allAlbum = AllAlbum.instance;

    // 检查是否已经初始化过
    if (allAlbum.albumList.isNotEmpty) {
      return; // 如果已经有数据，直接返回
    }

    AlbumList album1 = AlbumList(albumName: "周杰伦", albumCover: "images/cover_zhoujielun.png", musicList: []);
    album1.addMusic(MusicList(
        musicName: "album1music1",
        musicAuthor: "album1author1",
        musicPath: "album1path1"
    ));
    AlbumList album2 = AlbumList(albumName: "邓紫棋", albumCover: "images/cover_dengziqi.png", musicList: []);
    album2.addMusic(MusicList(
        musicName: "album2music1",
        musicAuthor: "album2author1",
        musicPath: "album2path1"
    ));
    allAlbum.addAlbum(album2);
    allAlbum.addAlbum(album1);
    allAlbum.sortAlbumsByName();
    allAlbum.printAllAlbum();
  }
}